<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if( __isAllowExport != 1){
	header("Location: ".basename($_SERVER["PHP_SELF"])."?op=List");
	exit();
}

$intLID = (int)$_data["LottoID"];
$intID = (int)$_data["ID"];
if($intLID <= 0 || $intID <= 0){
	$GLOBALS["errormessage"] = "Data Error!!";
	$GLOBALS["errorhidden"] = "\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error 1", "", 1);
}

$arrGame = getGambleDataByID($intID);
if(!is_array($arrGame) || sizeof($arrGame) == 0 || $arrGame["isEnd"] == 1 || $arrGame["GameDate"]." ".$arrGame["EndGameTime"] > date("Y-m-d H:i:s")){
	print $arrGame["isEnd"]." == 1 || ".$arrGame["GameDate"]." ".$arrGame["EndGameTime"]." > ".date("Y-m-d H:i:s");
	@mysql_close($$_Conn);
	header("Location: ".basename($_SERVER["PHP_SELF"])."?op=List");
	exit();
}

$strSQL = "select ID, LottoID, MemCommission as MC, Bet_Type, GambleID,  MemStake, SubIncome, isPayOff, WagerType from `bet_BetList` where `GambleID` = ".$intID." and `isPayOff` > -2";

if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Data Error!!";
	$GLOBALS["errorhidden"] = "Error:".PMA_mysql_error($$_Conn)."\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error 2", "", 1);
}

$arrRows = array();
$arrName = array();
$arrID = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrRows[$Rows -> ID] = $Rows;
	$arrID[] = $Rows -> ID;
}

$arrConRows = array();
if(sizeof($arrID) > 0){
	$strSQL = "select ID, BetDate as BD, BetTime as BT, Content, MemAccount, OneStake from`bet_BetListContent` where ID in (".join(",", $arrID).")";
	if($RS = @PMA_mysql_query($strSQL, $$_Conn)){
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrConRows[$Rows -> ID] = $Rows;
		}
	}
}

$arrWagerType = array(0 => "", 1 => "", 2 => "代理补货", 3 => "总代理补货", 4 => "股东补货", 5 => "总监补货");
$strCon = '"序号","下注日期","下注时间","账号","退水","下注种类","注单内容","注额","结果","方式","状态"';
foreach($arrRows as $intWID => $objRows){
	$arrTmp = array();

	$intLType = $_arrLotto[$objRows -> LottoID]["LType"];
	$objConRows = $arrConRows[$intWID];
	$arrTmp[0] = $objRows -> ID;
	$arrTmp[1] = DateForm($objConRows -> BD, $objConRows -> BT);
	$arrTmp[2] = TimeForm($objConRows -> BD, $objConRows -> BT);
	$arrTmp[3] = $objConRows -> MemAccount;
	$arrTmp[4] = 100 - $objRows -> MC*100;
	$arrTmp[5] = $_arrBet_Type[$intLType][$objRows -> Bet_Type];
	$arrTmp[6] = WagerFormTrans($objRows -> LottoID, $objRows -> Bet_Type, $objConRows -> Content);

	$arrTmp[6] = str_replace("<br />", "\n", $arrTmp[6]);
	$arrTmp[6] = str_replace("&nbsp;", " ", $arrTmp[6]);
	$arrTmp[6] = preg_replace("/<[^<>]+>/", "", $arrTmp[6]);

	$arrTmp[7] = sprintf("%.1f", $objRows -> MemStake);
	$arrTmp[8] = sprintf("%.1f", $objRows -> SubIncome);
	$arrTmp[9] = $arrWagerType[$objRows -> WagerType];
	$arrTmp[10] = $objRows -> isPayOff == "-1"?"注销":" ";

	foreach($arrTmp as $k => $v)$arrTmp[$k] = '"'.addslashes($v).'"';
	$strCon .= "\n".implode(',', $arrTmp);
}

$strConLen = strlen($strCon);
$strNow = date("Y-m-d H:i:s");
$strKey = md5("@@".crc32($strNow)."##".$strCon."&&".sha1("Export_".$intLID."_".$arrGame["SerialNo"])."==");

$strCon .= "\n\n\n\n\n\n\n".$strNow;
$strCon .= "\n".$strConLen;
$strCon .= "\n".$strKey;

if(!AddData_Log("", "匯出注單資料", "LID: ".$intLID.", \nGID: ".$intID.", \nSerialNo: ".$arrGame["SerialNo"].", \nNow: ".$strNow.", \nKey: ".$strKey.", \nLen: ".$strConLen)){
	$GLOBALS["errormessage"] = "Log Data Error!!";
	$GLOBALS["errorhidden"] = "Error:".PMA_mysql_error($$_Conn)."\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error 3", "", 1);
}

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=Export_".$intLID."_".$arrGame["SerialNo"].".csv");
print "\xEF\xBB\xBF".$strCon;
?>